package com.cj.library.widget.recyclerview.adapter;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.ColorInt;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by peter on 2016/9/9.
 */
public class CJDividerItemDecoration extends RecyclerView.ItemDecoration {

    private ColorDrawable mDivider;
    private int mDividerHeight = 3;
    private int mDividerColor;

    public CJDividerItemDecoration() {
        super();
        mDivider = new ColorDrawable(Color.GRAY);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (parent.getLayoutManager() instanceof GridLayoutManager) {
            int width = parent.getMeasuredWidth();
            int pdl = parent.getPaddingLeft();
            int pdr = parent.getPaddingRight();
            int count = parent.getChildCount();
            View child = null;
            for (int i = 0; i < count; i++) {
                child = parent.getChildAt(i);
                mDivider.setBounds(pdl, child.getBottom(), width - pdr, child.getBottom() + mDividerHeight);
                mDivider.draw(c);
                mDivider.setBounds(child.getRight(), child.getTop(), child.getRight() + mDividerHeight, child.getBottom());
                mDivider.draw(c);
            }
        } else if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            int width = parent.getMeasuredWidth();
            int pdl = parent.getPaddingLeft();
            int pdr = parent.getPaddingRight();
            int count = parent.getChildCount();
            View child = null;
            for (int i = 0; i < count; i++) {
                child = parent.getChildAt(i);
                mDivider.setBounds(pdl, child.getBottom(), width - pdr, child.getBottom() + mDividerHeight);
                mDivider.draw(c);
            }
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() instanceof GridLayoutManager) {
            outRect.set(0, 0, mDividerHeight, mDividerHeight);
        } else if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            outRect.set(0, 0, 0, mDividerHeight);
        }
    }

    public void setDivider(ColorDrawable mDivider) {
        this.mDivider = mDivider;
    }

    public void setDividerHeight(int mDividerHeight) {
        this.mDividerHeight = mDividerHeight;
    }

    public void setmDividerColor(@ColorInt int mDividerColor) {
        this.mDividerColor = mDividerColor;
    }
}
